package com.example.bootmanager.apk;

import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import java.util.HashMap;

public class ApkCacheHelper {
	private Context context;
	private boolean init = false;
	private HashMap<String, ApkCache> mCacheMap;
	private DbHelper mDbHelper;

	public ApkCacheHelper(Context paramContext) {
		this.context = paramContext;
		init(paramContext);
	}

	private void init(Context paramContext) {
		if (this.init)
			return;
		this.init = true;
		Debug.i("ApkCache +++");
		this.mDbHelper = new DbHelper(this.context);
		this.mCacheMap = new HashMap();
		Cursor localCursor = this.mDbHelper.getApkCacheCursor();
		if (localCursor != null)
			localCursor.moveToFirst();
		while (true) {
			if (localCursor.isAfterLast()) {
				localCursor.close();
				Debug.i("ApkCache ---");
				return;
			}
			ApkCache localApkCache = new ApkCache();
			localApkCache.appName = localCursor.getString(localCursor
					.getColumnIndex("appName"));
			localApkCache.pkgName = localCursor.getString(localCursor
					.getColumnIndex("pkg"));
			byte[] arrayOfByte = localCursor.getBlob(localCursor
					.getColumnIndex("icon"));
			if (arrayOfByte != null)
				localApkCache.icon = new BitmapDrawable(
						paramContext.getResources(),
						BitmapFactory.decodeByteArray(arrayOfByte, 0,
								arrayOfByte.length));
			this.mCacheMap.put(localApkCache.pkgName, localApkCache);
			localCursor.moveToNext();
		}
	}

	public ApkCache load(String paramString) {
		init(this.context);
		ApkCache localApkCache = (ApkCache) this.mCacheMap.get(paramString);
		return localApkCache;
	}

	public boolean save(String packageName, Drawable paramDrawable,
			String ApkInfoName) {
		init(this.context);
		ApkCache localApkCache = new ApkCache();
		localApkCache.appName = ApkInfoName;
		localApkCache.pkgName = packageName;
		localApkCache.icon = paramDrawable;
		if (this.mDbHelper.saveApkCache(localApkCache) != -1L) {
			this.mCacheMap.put(packageName, localApkCache);
			return true;
		}
		return false;
	}

	public static class ApkCache {
		public String appName;
		public Drawable icon;
		public String pkgName;
	}
}
